﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace Landscape {
    /// <summary>
    /// Defines the output settings used to render an XSL template
    /// </summary>
    class TemplateOutputSettings {
        /// <summary>
        /// The W3C version number for the output format (only used with method="html" or method="xml")
        /// </summary>
        public string Version {
            get;
            set;
        }

        /// <summary>
        /// Defines the output format. The default is XML 
        /// </summary>
        public OutputMethods Method {
            get;
            set;
        }

        /// <summary>
        /// Specifies whether the XML declaration should be included in the output.
        /// </summary>
        public bool OmitXmlDeclaration {
            get;
            set;
        }

        /// <summary>
        /// Specifies whether a standalone declaration should occur in the output
        /// </summary>
        public bool Standalone {
            get;
            set;
        }

        /// <summary>
        /// indicates whether the output should be indented according to its hierarchic structure
        /// </summary>
        public bool Indent {
            get;
            set;
        }

        /// <summary>
        /// Sets the value of the encoding attribute in the output
        /// </summary>
        public string Encoding {
            get;
            set;
        }

        /// <summary>
        /// Sets the value of the PUBLIC attribute of the DOCTYPE declaration in the output
        /// </summary>
        public string DocTypePublic {
            get;
            set;
        }

        /// <summary>
        /// Sets the value of the SYSTEM attribute of the DOCTYPE declaration in the output
        /// </summary>
        public string DocTypeSystem {
            get;
            set;
        }

        public TemplateOutputSettings() {

        }

        /// <summary>
        /// Creates an instance of TemplateOutputSettings representing an xsl:output element
        /// </summary>
        /// <param name="source">The XmlNode instance of xsl:output</param>
        public TemplateOutputSettings(XmlNode source)
            : base() {
            XPathNavigator navi = source.CreateNavigator();
            XPathNodeIterator attrs = navi.Select("attribute::*[string-length(string(.)) > 0]");
            while (attrs.MoveNext()) {
                string attrName = attrs.Current.Name;
                string attrValue = attrs.Current.Value;

                switch (attrName) {
                    case "method":
                        Landscape.OutputMethods tmp;
                        Method = Enum.TryParse<Landscape.OutputMethods>(attrValue, out tmp) ? tmp : Landscape.OutputMethods.xml;
                        break;
                    case "version":
                        Version = attrValue;
                        break;
                    case "encoding":
                        Encoding = attrValue;
                        break;
                    case "omit-xml-declaration":
                        OmitXmlDeclaration = attrValue.ToLower() == "yes" ? true : false;
                        break;
                    case "doctype-public":
                        DocTypePublic = attrValue;
                        break;
                    case "doctype-system":
                        DocTypePublic = attrValue;
                        break;
                    case "indent":
                        Indent = attrValue.ToLower() == "yes" ? true : false;
                        break;
                }
            }

        }
    }
}
